import { ConfigProvider } from 'antd';
import { render } from 'react-dom';
import zhCN from 'antd/lib/locale/zh_CN'
import App from './App';
import './style/index.less'
import { IUser } from './contexts/GlobalContext';
import { getUserDetail } from './services/api';



async function init() {

  const InitUser: IUser = {
    name: '',
    token: '',
    infoToken: '',
    account: '',
    id: Number.NaN,
    tenant_id: Number.NaN
  }
  
  // 首先读取缓存信息
  try {
    const info = await window.electron.ipcRenderer.getStoreValue('user-info')
    console.log('cache info ==', info)

    if(info && info.name !== '' && info.token !== '' && info.infoToken !== '') {
      const resp = await getUserDetail(info.infoToken)
      // console.log('resp 1==', resp)

      // 对用户信息赋值
      InitUser.token = info.token 
      InitUser.infoToken = info.infoToken
      InitUser.name = resp.data.params.realname
      InitUser.account = resp.data.params.account
      InitUser.id = resp.data.params.id
      InitUser.tenant_id = resp.data.params.tenant_id

      window.electron.log.info('user cache: ', info)

    }

  } catch(err: any) {
    console.log('read cache error =', err)

    if(err && err.response && err.response.status === 401) {
      // token 过期
      window.electron.log.info('get user infosimple err: ', err)
    } else {
      window.electron.log.info('user no cache: ', err)
    }
  }

  // 渲染页面
  render(
    <ConfigProvider locale={zhCN} >
      <App user={InitUser} />
    </ConfigProvider>,
  
    document.getElementById('root')
  );

}

init()




